package com.niit.common.component;



import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yaojianfeng
 */
//@Component
public class MyExceptionHandler implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView modelAndView = new ModelAndView();

        //进行异常类型匹配
        if (ex instanceof MyException){
            modelAndView.setViewName("exception/myException");
            modelAndView.addObject("name",ex.getClass().getSimpleName());
            modelAndView.addObject("message",((MyException) ex).getMsg());
            System.out.println("可以在这里进行自定义异常的逻辑处理");

         return modelAndView;
        }


        //匹配不到 就进入统一的异常页
        modelAndView.setViewName("exception/errorPage");
        return modelAndView;
    }
}
